package com.changgou.search.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //定义队列名称
    //更新广告缓存的消息队列
    //更新到openresty中
    public static final String AD_UPDATE_QUEUE = "ad_update_queue";

    @Bean("AD_UPDATE_QUEUE")
    public Queue AD_UPDATE_QUEUE(){
        return new Queue(AD_UPDATE_QUEUE);
    }

    //----------------------华丽的分割线--------------------------
    //上架相关操作

    //定义队列名称,添加商品到索引库中
    public static final String SEARCH_ADD_QUEUE = "search_add_queue";

    //定义交换机名称,上架
    public static final String GOODS_UP_EXCHANGE = "goods_up_exchange";


    //添加数据到索引库
    @Bean(SEARCH_ADD_QUEUE)
    public Queue SEARCH_ADD_QUEUE (){
        return new Queue(SEARCH_ADD_QUEUE);
    }

    @Bean(GOODS_UP_EXCHANGE)
    public Exchange GOODS_UP_EXCHANGE(){
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }

    //绑定交换机和队列
    @Bean
    public Binding GOODS_UP_EXCHANGE_BINDING(@Qualifier(SEARCH_ADD_QUEUE) Queue queue, @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }


    //----------------------华丽的分割线--------------------------
    //下架相关操作
    //定义队列名称
    public static final String SEARCH_DELETE_QUEUE = "search_delete_queue";

    //定义交换机名称,上架
    public static final String GOODS_DOWN_EXCHANGE = "goods_down_exchange";

    @Bean(SEARCH_DELETE_QUEUE)
    public Queue SEARCH_DELETE_QUEUE (){
        return new Queue(SEARCH_DELETE_QUEUE);
    }

    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange GOODS_DOWN_EXCHANGE(){
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }

    //绑定交换机和队列
    @Bean
    public Binding GOODS_DOWN_EXCHANGE_BINDING(@Qualifier(SEARCH_DELETE_QUEUE) Queue queue, @Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }


    //----------------------华丽的分割线--------------------------
    //生成静态化页面,绑定到上架队列
    public static final String PAGE_CREATE_QUEUE = "page_create_queue";

    //添加数据到索引库
    @Bean(PAGE_CREATE_QUEUE)
    public Queue PAGE_CREATE_QUEUE (){
        return new Queue(PAGE_CREATE_QUEUE);
    }

    //绑定交换机和队列
    @Bean
    public Binding PAGE_CREATE_QUEUE_BINDING(@Qualifier(PAGE_CREATE_QUEUE) Queue queue, @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

}
